package com.bodacn.keypoint;


import android.app.Activity;
import android.app.DatePickerDialog;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.graphics.Color;
import android.media.Ringtone;
import android.media.RingtoneManager;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.os.IBinder;
import android.os.Message;

import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import android.text.InputType;
import android.text.format.DateFormat;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.inputmethod.InputMethodManager;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import static android.view.ViewGroup.LayoutParams.MATCH_PARENT;


/**
 * Created by bodacn on 2016/6/15.
 */
public class FragmentScanInfo extends Fragment implements View.OnClickListener {
    private String TAG=this.getClass().getName();
    private final int MSG_DetailOver=1001;
    private final int MSG_HeadOver =1002;
    private final int MSG_GetDetail=1003;
    private final int MSG_FocusBarcode=1004;

    String STORE_NAME = "Settings";
    String curLoginUserNo,curLoginUser,curServiceUrl="",curServiceDir="",curDepartNo,curDepart,CPUID;
    private ProgressDialog waitDialog = null;
    Button btn_Query;
    ListView detailList;
    EditText edt_StartDate,edt_EndDate, edt_EmpNo;
    LinearLayout querylisthead;
    JSONArray detailJson,headJson;
    Toast curToast;
    String PrEmpNo="";
    //接口实现-------------------------------------------------------------------------->;
    //用于将Activity中的参数传给Fragment
    private Map<String,Object> fragmentParams=new HashMap<String,Object>();
    //用于与主Activity通讯;
    private Handler mainHandler;
    @Override
    public void onDetach() {
        super.onDetach();
        if (waitDialog!=null) waitDialog.dismiss();
    }


    public FragmentScanInfo()
    {
        System.out.println("FragmentScanInfo()");
    }




    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View root=inflater.inflate(R.layout.fragment_scaninfo,container,false);
        InitView(root);//按钮初始化;
        System.out.println("ProcFragment_onCreateView");
        return root;
    }


    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //创建对话框，
        waitDialog=new ProgressDialog(getContext());
        waitDialog.setTitle("请稍等");
        waitDialog.setIndeterminate(true);
        //初始提醒;
        curToast = Toast.makeText(getContext(), "", Toast.LENGTH_LONG);
        curToast.setGravity(Gravity.CENTER, 0, 0);
        //获取屏幕信息
        DisplayMetrics metric = new DisplayMetrics();
        getActivity().getWindowManager().getDefaultDisplay().getMetrics(metric);
        System.out.println("ProcFragment_onCreate");
        //得到Activity的Handler,用于发消息给主Activity;
        MainActivity activity=(MainActivity) getActivity();
        mainHandler=activity.mainHandler;
        //加接口监听
        activity.setTransScanInfoParamsListener(new MainActivity.OnTransParamsListenerScanInfo() {
            @Override
            public void TransParams(Map<String, Object> mParams) {
                if (mParams!=null) {
                    try {
                        String paramComm= mParams.get("ParamComm").toString();
                        if (paramComm=="LoginParam")
                        {
                            Log.d(this.getClass().getName(),"LoginParam------------------>1");
                            //传递登陆参数信息;
                            curLoginUserNo=mParams.get("LoginUserNo").toString();
                            curLoginUser=mParams.get("LoginUser").toString();
                            curServiceUrl=mParams.get("ServiceUrl").toString();
                            curServiceDir= mParams.get("ServiceDir").toString();
                            curDepartNo=mParams.get("DepartNo").toString();
                            curDepart=mParams.get("Depart").toString();
                            CPUID=mParams.get("CPUID").toString();
                            Log.d(this.getClass().getName(),"LoginParam------------------>2");
                            //发消息置焦点
                            Message msg =new Message();
                            msg.what = MSG_FocusBarcode;
                            fragmentHandler.removeMessages(msg.what);
                            fragmentHandler.sendMessage(msg);
                        }
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            }
        });
    }
    //插放音乐;
    private void playRingtone(){
        Uri notification = Uri.parse("android.resource://"+getActivity().getPackageName()+"/"+R.raw.scan);
        Ringtone r = RingtoneManager.getRingtone(getActivity(), notification);
        r.play();

    }

    //界面初始化;
    private void InitView(View v){
        //初始化界面
        btn_Query=v.findViewById(R.id.btn_Query);
        btn_Query.setOnClickListener(this);
        detailList=v.findViewById(R.id.detailList);
        //列表头;
        querylisthead=v.findViewById(R.id.querylisthead);
        edt_EmpNo =v.findViewById(R.id.edt_EmpNo);
        edt_EmpNo.setInputType(InputType.TYPE_NULL);
        edt_EmpNo.setOnKeyListener(new View.OnKeyListener() {
            @Override
            public boolean onKey(View v, int keyCode, KeyEvent event) {
                if (event!=null){
                    if ((event.getAction()==KeyEvent.ACTION_DOWN)&&(event.getKeyCode()== KeyEvent.KEYCODE_ENTER))
                    {
                        //虚拟键盘或实体键盘可考虑是否通过deviceID，或source来判断
                        // KeyEvent:KeyEvent { action=ACTION_DOWN, keyCode=KEYCODE_ENTER, scanCode=0, metaState=0, flags=0x16, repeatCount=0, eventTime=142515176, downTime=142515176, deviceId=-1, source=0x0 }
                        //隐藏键盘
                        hideIM(v);
                        playRingtone();
                        if (edt_EmpNo.getText().length()==0) {
                            curToast.setText(getString(R.string.PleaseInputBarcode));
                            curToast.show();
                        } else {
                            GetHeadList();
                        }
                        //未有焦点
                        if (edt_EmpNo.isFocused()==false){
                            //发消息置焦点
                            Message msg =new Message();
                            msg.what = MSG_FocusBarcode;
                            fragmentHandler.removeMessages(msg.what);
                            fragmentHandler.sendMessage(msg);
                        } else edt_EmpNo.selectAll();
                    }

                }
                return false;
            }
        });
        edt_EmpNo.setOnClickListener(this);
        edt_EmpNo.requestFocus();
        edt_EmpNo.requestFocusFromTouch();
        //文本框
        edt_StartDate = v.findViewById(R.id.edt_StartDate);
        edt_StartDate.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                final Calendar calendar = Calendar.getInstance();
                final DatePickerDialog mDialog = new DatePickerDialog(v.getContext(), null, calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH), calendar.get(Calendar.DAY_OF_MONTH));
                mDialog .setButton(DialogInterface.BUTTON_POSITIVE,"完成",new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        DatePicker datePicker = mDialog.getDatePicker();
                        int year = datePicker.getYear();
                        int month = datePicker.getMonth();
                        int day = datePicker.getDayOfMonth();
                        calendar.set(year,month,day);
                        edt_StartDate.setText(DateFormat.format("yyyy-MM-dd", calendar));
                    }
                });
                mDialog.setButton(DialogInterface.BUTTON_NEGATIVE,"取消",new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        System.out.println("cancel~~cancel~~");
                    }
                });
                mDialog.setTitle("选择查询起始日期");
                mDialog.show();

            }
        });
        edt_StartDate.setOnFocusChangeListener(new View.OnFocusChangeListener() {
            @Override
            public void onFocusChange(View v, boolean hasFocus) {
                Log.d("edit_startDate","onFocusChange");
                if (hasFocus == true) {
                    hideIM(v);
                }
            }
        });
        edt_EndDate = v.findViewById(R.id.edt_EndDate);
        edt_EndDate.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                final Calendar calendar = Calendar.getInstance();
                final DatePickerDialog mDialog = new DatePickerDialog(v.getContext(), null, calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH), calendar.get(Calendar.DAY_OF_MONTH));
                mDialog .setButton(DialogInterface.BUTTON_POSITIVE,"完成",new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        DatePicker datePicker = mDialog.getDatePicker();
                        int year = datePicker.getYear();
                        int month = datePicker.getMonth();
                        int day = datePicker.getDayOfMonth();
                        calendar.set(year,month,day);
                        edt_EndDate.setText(DateFormat.format("yyyy-MM-dd", calendar));
                    }
                });
                mDialog.setButton(DialogInterface.BUTTON_NEGATIVE,"取消",new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        System.out.println("cancel~~cancel~~");
                    }
                });
                mDialog.setTitle("选择查询截止日期");
                mDialog.show();
            }
        });

        //置日期初始值;
        Date curDate = new Date(System.currentTimeMillis());
        String EndDateStr=(String) DateFormat.format("yyyy-MM-dd", new Date(curDate.getTime()));
        edt_StartDate.setText(EndDateStr);
        edt_EndDate.setText(EndDateStr);

    }



    // 隐藏手机键盘
    private void hideIM(View edt) {
        try {
            InputMethodManager im = (InputMethodManager) getActivity().getSystemService(Activity.INPUT_METHOD_SERVICE);
            IBinder windowToken = edt.getWindowToken();
            if (windowToken != null) {
                im.hideSoftInputFromWindow(windowToken, 0);
            }
        } catch (Exception e) {

        }
    }
    public  String TransValue(String params)
    {
        if (params.toUpperCase().equals("NULL")||params.equals("0")||params.equals("0.0"))
            return "";
        else return params;
    }

    @Override
    public void onClick(View v) {
        hideIM(v);
        switch (v.getId()){
            case R.id.btn_Query:
                GetHeadList();
                break;
            case R.id.edt_EmpNo:
                edt_EmpNo.setInputType(InputType.TYPE_CLASS_TEXT);
                //1.得到InputMethodManager对象
                InputMethodManager imm = (InputMethodManager) getActivity().getSystemService(getActivity().INPUT_METHOD_SERVICE);
                //2.调用showSoftInput方法显示软键盘，其中view为聚焦的view组件
                imm.showSoftInput(edt_EmpNo,InputMethodManager.SHOW_FORCED);
                break;
            default:
                break;
        }

    }

    //获取明细;
    private void GetDetail() {
        if (edt_EmpNo.getText().toString().length()>0) PrEmpNo=edt_EmpNo.getText().toString();
        String sqlCmd="Exec KeyPoint_QueryScanInfo @OperType='Query',@StartDate='"+edt_StartDate.getText().toString()+"',@EndDate='"
                    +edt_EndDate.getText().toString()+"',@EmpNo='"+PrEmpNo+"',@UserNo='" + curLoginUserNo + "',@CPUID='" + CPUID + "'";
        ReadSQLData(sqlCmd);
        edt_EmpNo.setHint(PrEmpNo);
        edt_EmpNo.setText("");
        edt_EmpNo.setInputType(InputType.TYPE_NULL);
        hideIM(edt_EmpNo);
    }

    //提交所选择的工序至服务器,然后刷新工序列表;
    private void GetHeadList() {
        try {
            headJson=new JSONArray("[]");
        } catch (JSONException e) {
            e.printStackTrace();
        }
        Runnable run = new Runnable() {
            @Override
            public void run() {
                //将数据读到JSONArray;
                try {
                    String sqlcmd="Exec KeyPoint_QueryScanInfo @OperType='Title',@UserNo='" + curLoginUserNo + "',@CPUID='" + CPUID + "'";
                    Statement stmt = ToolUtil.getSQLConnection().createStatement();
                    ResultSet rs = stmt.executeQuery(sqlcmd);
                    //取字段相关信息;
                    ResultSetMetaData metaData = rs.getMetaData();
                    int columnCount = metaData.getColumnCount();
                    //遍列数据;
                    while (rs.next()) {
                        JSONObject jsonObj = new JSONObject();
                        // 遍历每一列
                        for (int i = 1; i <= columnCount; i++) {
                            String columnName = metaData.getColumnLabel(i);
                            Object value = rs.getObject(columnName);
                            if (value==null) value="";
                            jsonObj.put(columnName, value);
                        }
                        headJson.put(jsonObj);
                    }
                    //显示预排单;
                    Message msg = new Message();
                    msg.what = MSG_HeadOver;
                    fragmentHandler.removeMessages(msg.what);
                    fragmentHandler.sendMessage(msg);
                    //消息给创建列;
                } catch (JSONException e) {
                    e.printStackTrace();
                } catch (SQLException e) {
                    e.printStackTrace();
                }catch (Exception e){
                    e.printStackTrace();
                }


            }
        };
        new Thread(run).start();
    }
    //创建列;
    private void CreateListHead() {
        //内容为表头，宽度，自适应;
        if ((headJson != null) && (headJson.length() > 0)) {
            int childCount = querylisthead.getChildCount();
            for (int i = childCount - 1; i >= 0; i--) {
                View view = querylisthead.getChildAt(i);
                if (view instanceof TextView) {
                    querylisthead.removeViewAt(i);
                }
            }
            //创建列
            String TitleText,TitleName;
            float  txtWidth;
            int TxtSize,AutoZoom,Align;
            System.out.println(headJson);
            for (int i=0;i<headJson.length();i++)
                if (headJson.optJSONObject(i).has("TitleText") == true) {
                    try {
                        TitleName = headJson.optJSONObject(i).getString("TitleName");
                        TitleText = headJson.optJSONObject(i).getString("TitleText");
                        TxtSize = headJson.optJSONObject(i).getInt("TxtSize");
                        txtWidth = headJson.optJSONObject(i).getLong("TxtWidth");
                        AutoZoom = headJson.optJSONObject(i).getInt("AutoZoom");
                        TextView tv = new TextView(getContext());
                        tv.setText(TitleText);
                        tv.setWidth((int) txtWidth);
                        tv.setTextColor(getResources().getColor(R.color.text_main));
                        tv.setTextSize(TxtSize);
                        if (headJson.optJSONObject(i).has("Align")==true) {
                            Align = headJson.optJSONObject(i).getInt("Align");
                            switch (Align) {
                                case 0:
                                    tv.setGravity(Gravity.CENTER_VERTICAL | Gravity.LEFT);
                                    break;
                                case 1:
                                    tv.setGravity(Gravity.CENTER_VERTICAL | Gravity.CENTER_HORIZONTAL);
                                    break;
                                case 2:
                                    tv.setGravity(Gravity.CENTER_VERTICAL | Gravity.RIGHT);
                                    break;
                                default:
                                    tv.setGravity(Gravity.CENTER_VERTICAL);
                                    break;
                            }
                        }
                        //自定义文字颜色;
                        if (headJson.optJSONObject(i).has("TitleColor")==true){
                            tv.setTextColor(GetColorValue( headJson.optJSONObject(i).getString("TitleColor")));
                        }
                        //添加到View中;
                        if (AutoZoom==1) querylisthead.addView(tv,new LinearLayout.LayoutParams((int) txtWidth, MATCH_PARENT, 1));
                        else querylisthead.addView(tv);
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                }
            querylisthead.postInvalidate();
            //发消息查明细;
            Message msg = new Message();
            msg.what = MSG_GetDetail;
            fragmentHandler.removeMessages(msg.what);
            fragmentHandler.sendMessage(msg);
        }
    }
    //直接连获取数据，与Handler配套使用;
    private void ReadSQLData ( final String SQLComm){
        Runnable run = new Runnable() {
            @Override
            public void run() {
                try {
                    JSONArray jsonArray = ToolUtil.QuerySQL2JSON(SQLComm);
                    //往界面转数据;
                    if ((jsonArray != null) && (jsonArray.length() > 0)) {
                        if (jsonArray.optJSONObject(0).has("OperType") == true) {
                            //呼叫信息及库存警告
                            if (jsonArray.optJSONObject(0).getString("OperType").equals("Query") == true) {
                                detailJson = jsonArray;
                                //显示预排单;
                                Message msg = new Message();
                                msg.what = MSG_DetailOver;
                                fragmentHandler.removeMessages(msg.what);
                                fragmentHandler.sendMessage(msg);
                            }
                        }
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }

            }

        };
        new Thread(run).start();

    }


    /**
     * 定义一个Handler用于接收Fragment发出来的指令
     */
    public Handler fragmentHandler=new Handler(){
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            if(msg!=null){
                //消息处理;
                System.out.println("收到的消息是："+msg.what);
                int msgId = msg.what;
                switch (msgId) {
                    case MSG_FocusBarcode:
                        //任意消息来均置焦点
                        edt_EmpNo.selectAll();
                        edt_EmpNo.requestFocus();
                        edt_EmpNo.requestFocusFromTouch();
                        System.out.println("收到的消息是："+msg.what);
                    case MSG_HeadOver:
                        //创建列;
                        CreateListHead();
                        break;
                    case MSG_GetDetail:
                        //创建列;
                        GetDetail();
                        break;
                    case MSG_DetailOver:
                        System.out.println(detailJson);
                        //显示明细数据
                        DetailListAdapter detailListAdapter=new DetailListAdapter(getContext());
                        detailList.setAdapter(detailListAdapter);
                        break;
                    default:
                        break;
                }
            }
        }
    };


    //获取颜色
    private int TransColor(String paramColorStr){
        int rtn=0;
        if ((paramColorStr.indexOf("#")==0)&&(paramColorStr.length()>=7)){
            rtn=Color.parseColor(paramColorStr);
        }else {
            if (isNumer(paramColorStr)) {
                int testColor = Integer.parseInt(paramColorStr);
                int red = (testColor & 0xff0000) >> 16;
                int green = (testColor & 0x00ff00) >> 8;
                int blue = (testColor & 0x0000ff);
                rtn = Color.rgb(red, green, blue);
            } else {
                if (paramColorStr.equals("Black") == true)
                    rtn = Color.BLACK;
                if (paramColorStr.equals("Blue") == true)
                    rtn = Color.BLUE;
                if (paramColorStr.equals("Yellow") == true)
                    rtn = Color.YELLOW;
                if (paramColorStr.equals("Red") == true)
                    rtn = Color.RED;
                if (paramColorStr.equals("Green") == true)
                    rtn = Color.GREEN;
                if (paramColorStr.equals("Gray") == true)
                    rtn = Color.LTGRAY;
                if (paramColorStr.equals("White") == true)
                    rtn = Color.WHITE;
            }

        }
        return rtn;
    }

    //判断是否为数字
    public static boolean isNumer(String str) {
        Pattern pattern = Pattern.compile("-?[0-9]+\\.?[0-9]+");
        Matcher isNum = pattern.matcher(str);
        if (!isNum.matches()) {
            return false;
        }
        return true;
    }

    //订单表格适配器;
    public final class DetailListAdapter extends BaseAdapter {
        private LayoutInflater inflater;
        public DetailListAdapter(Context context) {
            inflater = (LayoutInflater) context
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        }
        @Override
        public int getCount() {
            return detailJson.length();
        }

        @Override
        public Object getItem(int position) {
            try {
                return detailJson.get(position);
            } catch (JSONException e) {
                e.printStackTrace();
            }
            return null;
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            View view = inflater.inflate(R.layout.fragment_scaninfo_listhead, null, false);
            //if (convertView == null) convertView = inflater.inflate(R.layout.activity_query_listhead, null, false);
            int pullVatListWidth=parent.getWidth();
            view.setLayoutParams(new ListView.LayoutParams(pullVatListWidth,38));
            int color = getResources().getColor(R.color.text_black);
            String tmpValue;
            try {
                Log.d("msg", "getView()a----------------------------------------------------->" + position);
                Log.d(TAG, "getView()----------------------------------------------------->" + position);
                if (detailJson.optJSONObject(position).has("ConColor") == true) {
                    tmpValue = detailJson.optJSONObject(position).getString("ConColor");
                    color=GetColorValue(tmpValue);
                }
                if (position%2==1) view.setBackgroundColor(getResources().getColor(R.color.content));
                for (int i = 0; i < headJson.length(); i++)
                    if (headJson.optJSONObject(i).has("TitleText") == true) {
                        try {
                            String TitleName = headJson.optJSONObject(i).getString("TitleName");
                            if (detailJson.optJSONObject(position).has(TitleName)==true) tmpValue= detailJson.optJSONObject(position).getString(TitleName);
                            else tmpValue="unReturn";
                            int TxtSize = headJson.optJSONObject(i).getInt("TxtSize");
                            float txtWidth = headJson.optJSONObject(i).getLong("TxtWidth");
                            int AutoZoom = headJson.optJSONObject(i).getInt("AutoZoom");
                            TextView tv = new TextView(view.getContext());
                            tv.setText(tmpValue);
                            tv.setWidth((int) txtWidth);
                            tv.setTextColor(color);
                            tv.setGravity(Gravity.CENTER_VERTICAL);
                            if (headJson.optJSONObject(i).has("Align")==true) {
                                int Align;
                                Align = headJson.optJSONObject(i).getInt("Align");
                                switch (Align) {
                                    case 0:
                                        tv.setGravity(Gravity.CENTER_VERTICAL | Gravity.LEFT);
                                        break;
                                    case 1:
                                        tv.setGravity(Gravity.CENTER_VERTICAL | Gravity.CENTER_HORIZONTAL);
                                        break;
                                    case 2:
                                        tv.setGravity(Gravity.CENTER_VERTICAL | Gravity.RIGHT);
                                        break;
                                    default:
                                        tv.setGravity(Gravity.CENTER_VERTICAL);
                                        break;
                                }
                            }
                            //自定义文字颜色;
                            if (headJson.optJSONObject(i).has("Color")==true){
                                tv.setTextColor(GetColorValue( headJson.optJSONObject(i).getString("Color")));
                            }
                            //自定负数是红色;
                            if ((headJson.optJSONObject(i).has("NgvColor")==true)&&(headJson.optJSONObject(i).has("NgvTrans")==true)){
                                if (headJson.optJSONObject(i).getString("NgvTrans").equals("YES")&&(isNumer(tmpValue)==true)){
                                    if (Float.valueOf(tmpValue)<0) tv.setTextColor(GetColorValue(headJson.optJSONObject(i).getString("NgvColor")));
                                }
                            }
                            tv.setTextSize(TxtSize);
                            //添加到View中;
                            if (AutoZoom == 1)
                                ((LinearLayout)view).addView(tv, new LinearLayout.LayoutParams((int) txtWidth, MATCH_PARENT, 1));
                            else ((LinearLayout)view).addView(tv);
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }
            } catch (JSONException e) {
                e.printStackTrace();
                System.out.println(e.getMessage());
            }
            Log.d("msg","DetailListAdapter_position:"+position);
            return view;
        }
    }


    //从字符串转颜色
    private int GetColorValue(String paramColor){
        int PenColor= Color.WHITE;
        String StrPenColor=paramColor;
        if ((StrPenColor.indexOf("#")==0)&&(StrPenColor.length()==7)){
            int testColor=Integer.parseInt(StrPenColor.substring(1),16);
            int red = (testColor & 0xff0000) >> 16;
            int green = (testColor & 0x00ff00) >> 8;
            int blue = (testColor & 0x0000ff);
            PenColor=Color.rgb(red, green, blue);
        }else {
            if (isNumer(StrPenColor)) {
                int testColor = Integer.parseInt(StrPenColor);
                int red = (testColor & 0xff0000) >> 16;
                int green = (testColor & 0x00ff00) >> 8;
                int blue = (testColor & 0x0000ff);
                PenColor = Color.rgb(red, green, blue);
            } else {
                if (StrPenColor.equals("Black") == true)
                    PenColor = Color.BLACK;
                if (StrPenColor.equals("Blue") == true)
                    PenColor = Color.BLUE;
                if (StrPenColor.equals("Yellow") == true)
                    PenColor = Color.YELLOW;
                if (StrPenColor.equals("Red") == true)
                    PenColor = Color.RED;
                if (StrPenColor.equals("Green") == true)
                    PenColor = Color.GREEN;
                if (StrPenColor.equals("Gray") == true)
                    PenColor = Color.LTGRAY;
                if (StrPenColor.equals("White") == true)
                    PenColor = Color.WHITE;
            }
        }
        return PenColor;
    }

    @Override
    public void onStart() {
        super.onStart();
        Log.d("msg", "ProcFragment.onStart-----------------重新开始");
    }

    @Override
    public void onResume() {
        super.onResume();
        Log.d("msg", "ProcFragment.onResume-----------------重新激活");
    }

    @Override
    public void onPause() {
        super.onPause();
        Log.d("msg", "onPause-----------------切换时暂停");
    }
}
